package com.powernode.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.constant.BusinessEnum;
import com.powernode.domain.Member;
import com.powernode.domain.ProdComm;
import com.powernode.ex.handler.BusinessException;
import com.powernode.feign.ProductMemberFeign;
import com.powernode.mapper.ProdCommMapper;
import com.powernode.model.Result;
import com.powernode.service.ProdCommService;
import com.powernode.vo.ProdCommData;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 贺
* @description 针对表【prod_comm(商品评论)】的数据库操作Service实现
* @createDate 2024-07-18 13:24:14
*/
@Service
public class ProdCommServiceImpl extends ServiceImpl<ProdCommMapper, ProdComm>
    implements ProdCommService{

    @Resource
    private ProdCommMapper prodCommMapper;
    @Resource
    private ProductMemberFeign productMemberFeign;

    /**
     * 审核或回复评论
     *
     * @param prodComm
     * @return
     */
    @Override
    public Boolean replyOrCheckProdComm(ProdComm prodComm) {
        // 获取回复内容
        String replyContent = prodComm.getReplyContent();
        // 判断回复内容是否为空
        if (StringUtils.hasText(replyContent)) {
            // 设置回复时间
            prodComm.setReplyTime(new Date());
            // 设置回复状态
            prodComm.setReplySts(1);
        }
        return prodCommMapper.updateById(prodComm) > 0;
    }

    /**
     * 小程序查询商品评论总览信息
     *
     * @param prodId
     * @return
     */
    @Override
    public ProdCommData queryWxProdCommData(Long prodId) {
        // 根据商品id查询评论总数
        Long allCount = prodCommMapper.selectCount(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getStatus, 1));
        // 查询好评数量
        Long goodCount = prodCommMapper.selectCount(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getStatus, 1)
                .eq(ProdComm::getEvaluate, 0));
        // 查询中评数量
        Long secondCount = prodCommMapper.selectCount(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getStatus, 1)
                .eq(ProdComm::getEvaluate, 1));
        // 查询差评数量
        Long badCount = prodCommMapper.selectCount(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getStatus, 1)
                .eq(ProdComm::getEvaluate, 2));
        // 查询有图评论数量
        Long picCount = prodCommMapper.selectCount(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getStatus, 1)
                .isNotNull(ProdComm::getPics));
        // 好评率
        BigDecimal goodLv = BigDecimal.ZERO;
        if (0 != allCount) {
            goodLv = new BigDecimal(goodCount).divide(new BigDecimal(allCount), 3, BigDecimal.ROUND_HALF_UP)
                    .multiply(BigDecimal.valueOf(100));
        }

        return ProdCommData.builder()
                .allCount(allCount)
                .goodCount(goodCount)
                .secondCount(secondCount)
                .badCount(badCount)
                .picCount(picCount)
                .goodLv(goodLv)
                .build();
    }

    /**
     * 小程序分页查询单个商品的评论列表
     *
     * @param current
     * @param size
     * @param prodId
     * @param evaluate
     * @return
     */
    @Override
    public Page<ProdComm> queryWxProdCommPageByProdId(Long current, Long size, Long prodId, Integer evaluate) {
        // 1. 根据商品id分页查询单个商品的评论列表
        Page<ProdComm> page = new Page<>(current, size);
        page = prodCommMapper.selectPage(page, new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getStatus, 1)
                .eq(0 == evaluate || 1 == evaluate || 2 == evaluate, ProdComm::getEvaluate, evaluate)
                .isNotNull(3 == evaluate, ProdComm::getPics)
                .orderByDesc(ProdComm::getCreateTime));

        // 2. 从分页对象中获取评论集合
        List<ProdComm> prodCommList = page.getRecords();
        // 判断评论集合是否为空
        if (CollectionUtil.isEmpty(prodCommList) || prodCommList.size() == 0) {
            return page;
        }
        // 3. 从评论集合中获取会员openId集合
        List<String> openIdList = prodCommList.stream().map(ProdComm::getOpenId).distinct().collect(Collectors.toList());
        // 4. 远程调用：根据会员openId集合查询会员对象集合
        Result<List<Member>> memberListResult = productMemberFeign.getMemberListByOpenIdList(openIdList);
        // 判断远程调用是否成功
        if (BusinessEnum.OPERATION_FAIL.getCode().equals(memberListResult.getCode())) {
            throw new BusinessException("调用会员服务失败：根据会员openId集合查询会员对象集合");
        }
        // 5. 获取会员对象集合
        List<Member> memberList = memberListResult.getData();
        // 6. 遍历评论集合，设置每条评论的会员昵称和会员头像
        prodCommList.forEach(prodComm -> {
            // 6.1 获取会员对象
            Member member = memberList.stream().filter(m -> m.getOpenId().equals(prodComm.getOpenId())).findFirst().orElse(null);
            // 6.2 判断会员对象是否为空
            if (null != member) {
                // 6.3 会员昵称脱敏处理
                StringBuilder sb = new StringBuilder(member.getNickName());
                StringBuilder replaceNickName = sb.replace(1, sb.length() - 1, "***");

                // 6.4 设置会员昵称
                prodComm.setNickName(replaceNickName.toString());
                // 6.5 设置会员头像
                prodComm.setPic(member.getPic());
            }
        });
        return page;
    }
}




